﻿@using System.Text.RegularExpressions

@if (!string.IsNullOrEmpty(Title))
{
    <h3 id="@_ariaId">@Title</h3>
}

@if (Description != null)
{
    <div class="section-description">
        @Description
    </div>
}

<div class="demo-section-content">

    <FluentTabs ActiveTabId="@(!HideExample ? $"tab-example-{_ariaId}" : $"tab-razor-{_ariaId}")" fixed>
        @if (!HideExample)
        {
            <FluentTab Id="@("tab-example-"+@_ariaId)" Label="Example" fixed>
                <Content>
                    <div class="demo-section-example">
                        @ChildContent
                        @if (Component is not null)
                        {
                            <ErrorBoundary @ref="errorBoundary">
                                <ChildContent>
                                    <DynamicComponent Type="Component" Parameters="ComponentParameters" />
                                </ChildContent>
                                <ErrorContent Context="ex">
                                    <div class="blazor-error-boundary">@ex.Message</div>
                                </ErrorContent>
                            </ErrorBoundary>
                        }
                    </div>
                </Content>
            </FluentTab>
        }

        @if (!HideAllButExample && _hasCode)
        {
            @foreach (KeyValuePair<string, string> pair in _tabPanelsContent)
            {
                string tab = Path.GetExtension(pair.Key);
                string tabname = Regex.Replace(GetDisplayName(tab), "[#|/|\\|$|*|^]", "");

                string label = string.Empty;
                string id = string.Empty;

                if (pair.Key.StartsWith(Component.Name))
                {
                    label = GetDisplayName(tab);
                    id = "tab-" + tabname + "-" + @_ariaId;
                }
                else
                {
                    label = Path.GetFileName(pair.Key);
                    id = "tab-" + Regex.Replace(label, "[#|/|\\|$|*|^]", "").ToLower() + "-" + @_ariaId;
                }
                <FluentTab Id="@id" Label="@label" fixed>
                    <Content>
                        <CodeSnippet Style="@(!string.IsNullOrEmpty(MaxHeight) ? $"max-height: {MaxHeight};" : null)" Language="@TabLanguageClass(tab)">@pair.Value</CodeSnippet>
                    </Content>
                </FluentTab>
            }
        }
    </FluentTabs>
</div>
@if (ShowDownloads)
{
    <div class="demo-section-downloads">
        <FluentSpacer />
        <div style="display: flex; align-items: center;">
            Download:&nbsp;
            @foreach (KeyValuePair<string, string> pair in _tabPanelsContent)
            {
                string source = pair.Key;
                <FluentAnchor Style="min-width: 85px; text-overflow: ellipsis" Download="@source" Href="@($"_content/FluentUI.Demo.Shared/sources/{source}.txt")" Appearance="Appearance.Neutral">
                    @if (source.StartsWith(Component.Name))
                    {
                        @GetDisplayName(source.Substring(source.LastIndexOf('.')))
                    }
                    else
                    {
                        @source.Substring(0, source.LastIndexOf('.'))
                    }
                </FluentAnchor>

                @if (source != _tabPanelsContent.Last().Key)
                {
                    <span>&nbsp;</span>
                }
            }
        </div>
    </div>
}